<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/12
 * Time: 11:40
 */
namespace App\Repositories;

use App\Models\RepaymentAccountWithdraw;

class RepaymentAccountWithdrawRepository
{
    /**
     * 创建用户收益
     * @param array $params
     * @return mixed
     */
    public function create(array $params)
    {
        return RepaymentAccountWithdraw::create($params);
    }

    /**
     * 查找一条提现记录
     * @param $id
     * @return mixed
     */
    public function byId($id)
    {
        return RepaymentAccountWithdraw::find($id);
    }

    public function byWithdrawNo($orderNo)
    {
        return RepaymentAccountWithdraw::where('withdraw_no', $orderNo)->first();
    }

    public function updateStatusByWithdrawNo($orderNo, $status)
    {
        return RepaymentAccountWithdraw::where('withdraw_no', $orderNo)->update([
            'withdraw_status' => $status,
            'update_time' => time()
        ]);
    }

    public function getTradeRecordAll($userId, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        return RepaymentAccountWithdraw::where('user_id', $userId)
            ->offset($page)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();
    }

    public function getTradeRecordToStatus($userId, $status, $page, $limit)
    {
        $page -= 1;
        $page = $page < 0 ? 0 : $page;
        return RepaymentAccountWithdraw::where([
            'user_id' => $userId,
            'withdraw_status' => $status,
        ])
            ->offset($page)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();
    }

}